package com.foreknow.demo5;

import com.sun.org.apache.regexp.internal.REUtil;

import javax.sound.midi.Soundbank;
import java.util.Scanner;

public class BankSystem2 {
    public static void main(String[] args) {
        //初始化用户的信息(用户名、密码、余额)
        String userName = "admin";
        String userPwd = "123";
        double money = 10000.0;
        boolean isOpera = true;
        Scanner scan = new Scanner(System.in);
        //显示菜单 调用menu()方法
        getMenu();
        //登录调用login方法
        boolean isRight = usrLogin(userName,userPwd);
        //登录成功后可以循环操作
        while (isRight){
            while (isOpera){
                System.out.print("1.查询余额\n2.存款\n3.取款\n4.修改密码\n5.退出\n");
                System.out.println("请选择菜单");
                int choice = scan.nextInt();
                switch (choice){
                    case 1:
                        getAccount(money);
                        break;
                    case 2:
                        System.out.println("请输入存款金额");
                        double setMoney = scan.nextDouble();
                        money += setAccount(setMoney);
                        System.out.println("存款成功，余额为" + money);
                        break;
                    case 3:
                        System.out.println("取款");
                        break;
                    case 4:
                        System.out.println("修改密码");
                        break;
                    case 5:
                        System.out.println("退出成功");
                        break;
                }
                getOperate();
            }
        }
    }
    //获取菜单
    public static void getMenu(){
        System.out.println("*************欢迎登录*************");
        System.out.println("***********银行************");
        System.out.println("**************版本1.0**************");
    }
    //登录校验
    public static boolean usrLogin(String uName,String uPwd){
        Scanner input = new Scanner(System.in);
        for (int i = 3; i > 0;i--){
            System.out.println("请输入账号");
            String inputUsrName = input.next();
            System.out.println("请输入密码");
            String inputUsrPwd = input.next();
            //判断参数与用户接收到的用户名是否匹配
            if (uName.equals(inputUsrName) && uPwd.equals(uPwd)){
                System.out.println("登录成功");
                return true;
            }else {
                if (i-1 == 0){
                    System.out.println("密码错误3次，吞卡");
                    System.exit(0);
                }else {
                    System.out.println("用户名或者密码错误，您还有" + (i-1) +"次机会");
                }
            }
        }
        return false;
    }
    //查询余额

    /**
     *
     * @param money
     */
    public static void getAccount(double money){
        System.out.println("您的余额为" + money);
    }
    //获取操作
    public static void getOperate(){
        Scanner scan = new Scanner(System.in);
        System.out.println("是否继续操作(y/n)");
        if (scan.next().equals("n")){
            System.exit(0);
        }

    }
    //存款
    public static double setAccount(double x){
        if (x > 20000){
            System.out.println("单次存款金额超过1万");
            return 0;
        }else if (x < 0){
            System.out.println("存款金额错误");
            return 0;
        }else if (x %100 != 0){
            System.out.println("请输入100的整数倍存款");
            return 0;
        }
        return x;
    }
}
